//登录后才可以显示页面
//如果没有登录， 或者登录过期，直接跳转到 登录页

//获取元素
const cartBox = document.querySelector('.cartBox');
const totalPriceBox = document.querySelector('.totalPrice');
const totalNum = document.querySelector('.totalNum');
const allSelect = document.querySelector('.allSelect');
const content = document.querySelector('.content');
 var liss= JSON.parse(window.localStorage.getItem('cart'))


const id = window.localStorage.getItem('id');


testLogin()
function testLogin(){
    const login = window.localStorage.getItem('login');
    const login_co = getcookie('login')
    if(login !== '1'){
        window.localStorage.setItem('url','cart');
        window.location.href = './login.html';
        return;
    }
    if(!login_co){
        window.localStorage.setItem('url','cart');
        window.location.href = './login.html';
        return;
    }

    bindHtml();
}




function bindHtml(){
    let str = ''
    liss.forEach(item=>{
        str += `
        <li>
            <div class="select">
                <input data-id="${item.id}" class="select_item" type="checkbox" ${item.is_select ? 'checked' : ''}>
            </div>
            <div class="show">
                <img src="${item.image}" alt="">
            </div>
            <div class="title">
                ${item.name}
            </div>
            <div class="price">
                ￥ ${item.jdPrice}
            </div>
            <div class="number">
                <button class="jian" data-id="${item.id}">-</button>
                <input type="text" value="${item.buy_num}">
                <button class="add" data-id="${item.id}">+</button>
            </div>
            <div class="subprice">
                ￥ ${item.jdPrice * item.buy_num}
            </div>
            <div class="del">
                <button class="dell" data-id="${item.id}">删除</button>
            </div>
        </li>
        `
       
               
    })
    cartBox.innerHTML = str ;
    // + (abc ? ste : '')

    var selectNum = 0, buyNum = 0 , totalPrice = 0;
        liss.forEach(item=>{
        if(item.is_select === true){
            selectNum++;
            buyNum += item.buy_num;
            totalPrice += item.buy_num * item.jdPrice;
        }
    })
    allSelect.checked = liss.length === selectNum;
    totalNum.innerHTML = '购买总数 : ' + buyNum ;
    totalPriceBox.innerHTML = '总价格 : '+ totalPrice.toFixed(2);

    //全选
    const btn_item = document.querySelectorAll('.select_item')
    allSelect.addEventListener('click' , function(){
    
        btn_item .forEach( function(item){
            
            item.checked = allSelect.checked ;
        })
    })
    btn_item.forEach(function(item){
        item.addEventListener( 'click' , function(){
            var bool = true;
            for( var i = 0 ; i <= btn_item.length-1 ; i++ ){
                if( btn_item[i].checked === false ){
                    bool = false;
                    break;
                }
            }
            allSelect.checked = bool;
        })
    })
}



//点击事件
content.addEventListener('click', async e => {
    e = e || window.event
    const target = e.target || e.srcElement
       
    //  判断修改数量增加
    if (target.className === 'add') {
      // 拿到商品 id 和目前的购买数量
      const goods_id = target.dataset.id -0
      const number = target.previousElementSibling.value - 0 +1
      const abc = liss.findIndex(function(item){return item.id === goods_id})
      liss[abc].buy_num = number;
      if(liss[abc].buy_num > liss[abc].all_num){
        liss[abc].buy_num = liss[abc].all_num
      }
      bindHtml();
    }
    //判断数量减少
    if (target.className === 'jian') {
        // 拿到商品 id 和目前的购买数量
        const goods_id = target.dataset.id -0
        const number = target.nextElementSibling.value - 0 - 1
        if(number < 1){ number = 1 }
        const abc = liss.findIndex(function(item){return item.id === goods_id})
        liss[abc].buy_num = number;
        bindHtml();
    }
    //判断数据删除
    if (target.className === 'dell') {
        const goods_id = target.dataset.id - 0
        const newde = liss.filter(function(item){
            return item.id !== goods_id
        })
        liss = newde;
        bindHtml();
    }
    // 判断数据选中
    if (target.className === 'select_item') {
        // 点击的是选中按钮
        // 拿到商品 id
        const goods_id = target.dataset.id -0;

        const qwe = liss.findIndex(function(item){return item.id === goods_id})
        
        if(liss[qwe].is_select === true){
            liss[qwe].is_select = false;
            // liss[qwe].buy_num = 0;
        }else{
            liss[qwe].is_select = true;
            const alll = target.parentElement.parentElement.children[4].children[1].value - 0
            liss[qwe].buy_num = alll;
        }
        bindHtml();
    }
    if (target.className === 'del_all') {
        liss = [];
        bindHtml();
    }
    if (target.className === 'del_alls') {
        const goods_id = target.dataset.id - 0
        const newdel = liss.filter(function(item){
            return item.is_select === false;
        })
        liss = newdel;
        bindHtml();
    }
    if (target.className === 'look') {
        window.location.href = './list.html'
    }
    if (target.className === 'payy') {
        window.alert(`请支付现金`)
    }
})

